package com.zkingsoft.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import com.zkingsoft.bean.BizActivities;
import com.zkingsoft.bean.BizCollage;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.common.bean.SysUsers;
import com.zkingsoft.common.constance.AppConstance;
import com.zkingsoft.dao.BizActivitiesDao;
import com.zkingsoft.dao.BizCollageDao;
import com.zkingsoft.service.BizActivitiesService;

/**
 * @description service接口实现类(活动)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Service
public class  BizActivitiesServiceImpl implements  BizActivitiesService{

	
	@Autowired
	private BizActivitiesDao bizActivitiesDao;
	@Autowired
	private BizCollageDao bizCollageDao;
	
	@Override
	public int add(BizActivities bizActivities){
		// 设置基本字段信息
		SysUsers loginUser = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
		bizActivities.setCreateBy(loginUser.getSuName());
		bizActivities.setUpdateBy(loginUser.getSuName());
		bizActivities.setActId(UUIDUtil.getRandomID());
		return bizActivitiesDao.insert(bizActivities);
		
	}
	
	@Override
	public int batchAdd(List<BizActivities>  bizActivitiesList) {
		//这里没有做基本字段的设置，如有需要请自己实现	
		int num = 0;
		int c = 10000;
		int size = bizActivitiesList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= bizActivitiesList.size() ? bizActivitiesList.size() : end;
			List<BizActivities> insertList = bizActivitiesList.subList(begin, end);
			num += bizActivitiesDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(BizActivities oldBizActivities
	,BizActivities newBizActivities){
	
		Map<String, Object> modifyMap = null;
		try {
			if (!ModelUtils.isModified(oldBizActivities, newBizActivities)) {
				return MatrixConstance.DML_SUCCESSS;
			}
			modifyMap = ModelUtils.comparePojo2Map(oldBizActivities, newBizActivities);
		} catch (Exception e) {
			throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newBizActivities);
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("actId", oldBizActivities.getActId());
			bizActivitiesDao.updateByMap(modifyMap);
		}
		return MatrixConstance.DML_SUCCESSS;
	}
	
	@Override
	public int modifyByModel(BizActivities bizActivities){
	
		return bizActivitiesDao.updateByModel(bizActivities);
	
	}
	
	
	
	@Override
	public int remove(List<String> list){
	
		return bizActivitiesDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(String actId){
	
		return bizActivitiesDao.deleteById(actId);
	
	}
	
	@Override
	public int removeByModel(BizActivities bizActivities){
	
		return bizActivitiesDao.deleteByModel(bizActivities);
	
	}
	
	
	@Override
	public List<BizActivities> findInPage(BizActivities bizActivities,  PaginationVO pageVo){
	
		return bizActivitiesDao.selectInPage(bizActivities , pageVo);
	
	}
	
	@Override
	public List<BizActivities> findByModel(BizActivities bizActivities){
	
		return bizActivitiesDao.selectByModel(bizActivities);
	
	}
	
	@Override
	public int  findTotal(BizActivities bizActivities){
	
		return bizActivitiesDao.selectTotalRecord(bizActivities);
	
	}
	
	@Override
	public BizActivities  findById(String actId){
	
		return bizActivitiesDao.selectById(actId);
	
	}
	

	/**
	 * 删除活动
	 */
	@Override
	public int delActivities(BizActivities bizActivities, BizUser loginUser) {
		BizActivities activities=new BizActivities();
		activities.setActId(bizActivities.getActId());
		activities.setUserId(loginUser.getUserId());
		activities.setUpdateBy(loginUser.getUserId());
		activities.setActStatus(AppConstance.ACTIVITIES_STATUS_DEL);
		return bizActivitiesDao.updateByModel(bizActivities);
	}

	@Override
	public List<BizActivities> queryInPage(BizActivities bizActivities) {
		return bizActivitiesDao.queryInPage(bizActivities);
	}

	@Override
	public int queryTotalRecord(BizActivities bizActivities) {
		return bizActivitiesDao.queryTotalRecord(bizActivities);
	}

	@Override
	public BizActivities queryById(String actId) {
		return bizActivitiesDao.queryById(actId);
	}


	/**
	 * 上下架活动
	 */
	@Override
	public int upperOrLower(BizActivities bizActivities) {
		BizActivities activities=new BizActivities();
		activities.setUserId(bizActivities.getUserId());
		activities.setActId(bizActivities.getActId());
		int i=0;
		if(AppConstance.ACTIVITIES_STATUS_UP==bizActivities.getActStatus()){
			//上架
			activities.setActStatus(AppConstance.ACTIVITIES_STATUS_UP);
			i=bizActivitiesDao.updateByModel(activities);
		}else if(AppConstance.ACTIVITIES_STATUS_DOWN==bizActivities.getActStatus()){
			//下架
			activities.setActStatus(AppConstance.ACTIVITIES_STATUS_DOWN);
			i=bizActivitiesDao.updateByModel(activities);
		}
		return i;
	}
	
	
}